<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true" :model="listQuery" size="small">
        <el-form-item label="账号">
          <el-input
            v-model="listQuery.keyword"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="search" size="small"
            >查询</el-button
          >
          <el-button @click="reset" size="small">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            :loading="downloadLoading"
            type="success"
            icon="el-icon-plus"
            @click="handleEdit"
            size="small"
          >
            新增账号
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      key="id"
      v-loading="listLoading"
      :data="list"
      fit
      highlight-current-row
    >
      <el-table-column label="序号" type="index" width="50"> </el-table-column>

      <el-table-column label="账号" prop="user_name" show-overflow-tooltip>
      </el-table-column>

      <el-table-column label="账号描述" prop="desc" show-overflow-tooltip>
      </el-table-column>

      <el-table-column label="联系方式" prop="mobile"> </el-table-column>

      <el-table-column label="操作" class-name="small-padding fixed-width">
        <template slot-scope="{ row, $index }">
          <el-button
            v-if="row.user_name != 'admin'"
            type="text"
            size="mini"
            @click="handleEdit(row)"
          >
            编辑
          </el-button>
          <el-button
            style="margin-left: 10px"
            slot="reference"
            v-if="row.user_name != 'admin'"
            class="red"
            type="text"
            size="mini"
            @click="remove(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />

    <el-dialog width="500px" :title="title" :visible.sync="showDialog">
      <el-form :model="ruleForm" label-position="right" label-width="100px">
        <el-form-item label="账号" prop="timestamp">
          <el-input v-model="ruleForm.user_name"></el-input>
        </el-form-item>
        <el-form-item label="账号描述" prop="desc">
          <el-input v-model="ruleForm.desc"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="repassword">
          <el-input type="password" v-model="ruleForm.repassword"></el-input>
        </el-form-item>
        <el-form-item label="联系方式" prop="mobile">
          <el-input v-model="ruleForm.mobile"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList, remove, getInfo, add, update } from "@/api/account";
import Pagination from "@/components/Pagination";

export default {
  name: "account",
  components: { Pagination },
  data() {
    return {
      title: "新增",
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20,
        keyword: undefined,
      },
      ruleForm: {
        user_name: "",
        password: "",
        mobile: "",
        repassword: "",
        desc: "",
      },
      showDialog: false,
      downloadLoading: false,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      this.listLoading = true;
      const res = await fetchList(this.listQuery);
      if (res.data) {
        this.list = res.data.list;
        this.total = res.data.pageParam.total;
      }
      this.listLoading = false;
    },

    // 查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    reset() {
      this.listQuery = {
        page: 1,
        pageSize: 20,
        keyword: undefined,
      };
      this.getList();
    },
    async getInfo(admin_id) {
      const res = await getInfo({ admin_id });
      if (res.data) {
        this.ruleForm = res.data;
      }
    },
    // 编辑
    handleEdit(row) {
      if (row.admin_id) {
        this.getInfo(row.admin_id);
        this.title = "编辑";
      }
      this.showDialog = true;
    },
    async remove(row) {
      this.$confirm("确定删除吗？", "提示").then(async () => {
        const res = await remove({ id: row.admin_id });
        if (res) {
          this.$message({
            message: res.msg,
            type: "success",
          });
          this.getList();
        }
      });
    },
    cancel() {
      this.ruleForm = {
        user_name: "",
        password: "",
        mobile: "",
        repassword: "",
        desc: "",
      };
      this.title = "新增";
      this.showDialog = false;
    },
    async submitForm() {
      const postData = Object.assign({}, this.ruleForm);
      const func = this.ruleForm.admin_id ? update : add;
      const res = await func(postData);
      if (res) {
        this.$message({
          message: res.msg,
          type: "success",
        });
        this.cancel();
        this.getList();
      }
    },
  },
};
</script>
